package com.teamcenter.rac.workflow.common;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;

import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.common.tcviewer.TCViewerPanel;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentProcess;
import com.teamcenter.rac.kernel.TCComponentTask;
import com.teamcenter.rac.kernel.TCComponentType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCTaskState;
import com.teamcenter.rac.util.Instancer;
import com.teamcenter.rac.util.InterfaceExceptionStack;
import com.teamcenter.rac.util.InterfaceSignalOnClose;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.Registry;
import com.teamcenter.rac.util.Separator;
import com.teamcenter.rac.util.SignalOnClose;
import com.teamcenter.rac.util.viewer.AbstractViewPanel;
import com.teamcenter.rac.workflow.commands.formtask.FormTaskPanel;
import com.teamcenter.rac.workflow.processviewer.ProcessViewerApplicationPanel;

public class ProcessViewer
  extends AbstractViewPanel
  implements InterfaceSignalOnClose
{
  public static String TASK_VIEW;
  public static String PROCESS_VIEW;
  private ProcessViewerApplicationPanel processViewPanel = null;
  private Object target = null;
  private static boolean taskView = true;
  private JPanel radioPanel = null;
  private JPanel taskDisplayPanel = null;
  private JRadioButton taskButton = null;
  private JRadioButton processButton = null;
  private static boolean preferenceRead = false;
  
  public ProcessViewer(TCViewerPanel paramTCViewerPanel)
  {
    super(paramTCViewerPanel);
    setLayout(new BorderLayout());
    initializePanel();
  }
  
  public void setComponent(Object paramObject)
  {
    Object localObject1;
    Object localObject2;
    if (!preferenceRead)
    {
      preferenceRead = true;
      localObject1 = null;
      if (((paramObject instanceof TCComponentTask)) || ((paramObject instanceof TCComponentProcess))) {
        localObject1 = ((TCComponent)paramObject).getSession();
      }
      localObject2 = ((TCSession)localObject1).getPreferenceService();
      String str = ((TCPreferenceService)localObject2).getString(1, "TCVIEWER_default_workflow_view");
      if ((str == null) || (str.length() == 0) || (str.trim().toLowerCase().equals("task")))
      {
        taskView = true;
        this.taskButton.setSelected(true);
      }
      else
      {
        taskView = false;
        this.processButton.setSelected(true);
      }
    }
    if ((taskView) && ((paramObject instanceof TCComponentTask)))
    {
      localObject1 = (TCComponentTask)paramObject;
      localObject2 = new TaskLoadOperation((TCComponentTask)localObject1);
      ((TCComponentTask)localObject1).getSession().queueOperation((AbstractAIFOperation)localObject2);
      return;
    }
    boolean bool = true;
    if ((paramObject instanceof TCComponentProcess)) {
      bool = false;
    } else {
      try
      {
        ((TCComponentTask)paramObject).setViewedByMe(true);
      }
      catch (TCException localTCException)
      {
        MessageBox.post(localTCException);
      }
    }
    this.target = paramObject;
    if (this.taskDisplayPanel != null)
    {
      SignalOnClose.close(this.taskDisplayPanel);
      this.taskDisplayPanel = null;
    }
    if (this.processViewPanel == null)
    {
      TCSession localTCSession = ((TCComponent)this.target).getSession();
      this.processViewPanel = new ProcessViewerApplicationPanel(localTCSession);
      this.processViewPanel.initializeDisplay();
    }
    SwingUtilities.invokeLater(new SetProcessViewPanelRunnable(bool));
  }
  
  public void closeSignaled()
  {
    if ((taskView) && (this.processViewPanel != null)) {
      SignalOnClose.close(this.processViewPanel);
    }
  }
  
  private void initializePanel()
  {
    setTaskProcessLabels();
    this.taskButton = new JRadioButton(TASK_VIEW);
    this.taskButton.setActionCommand(TASK_VIEW);
    this.processButton = new JRadioButton(PROCESS_VIEW);
    this.processButton.setActionCommand(PROCESS_VIEW);
    ButtonGroup localButtonGroup = new ButtonGroup();
    localButtonGroup.add(this.taskButton);
    localButtonGroup.add(this.processButton);
    if (taskView) {
      this.taskButton.setSelected(true);
    } else {
      this.processButton.setSelected(true);
    }
    RadioListener localRadioListener = new RadioListener();
    this.taskButton.addActionListener(localRadioListener);
    this.processButton.addActionListener(localRadioListener);
    this.radioPanel = new JPanel(new BorderLayout());
    JPanel localJPanel = new JPanel();
    localJPanel.add(this.taskButton);
    localJPanel.add(this.processButton);
    this.radioPanel.add("North", localJPanel);
    this.radioPanel.add("South", new Separator());
  }
  
  private void setTaskProcessLabels()
  {
    this.registry = Registry.getRegistry(this);
    TASK_VIEW = this.registry.getString("TASK_VIEW");
    PROCESS_VIEW = this.registry.getString("PROCESS_VIEW");
  }
  
  public ProcessViewerApplicationPanel getProcessViewPanel()
  {
    return this.processViewPanel;
  }
  
  public Object[] getTargetObjects()
  {
    AIFComponentContext[] arrayOfAIFComponentContext = null;
    if ((this.processViewPanel != null) && (this.processViewPanel.isShowing())) {
      arrayOfAIFComponentContext = this.processViewPanel.getSelectedContextObjects();
    }
    return arrayOfAIFComponentContext;
  }
  
  public class TaskLoadOperation
    extends AbstractAIFOperation
  {
    private TCComponentTask task;
    
    public TaskLoadOperation(TCComponentTask paramTCComponentTask)
    {
      this.task = paramTCComponentTask;
    }
    
    public void executeOperation()
    {
      if (ProcessViewer.this.taskDisplayPanel != null)
      {
        SignalOnClose.close(ProcessViewer.this.taskDisplayPanel);
        ProcessViewer.this.taskDisplayPanel = null;
      }
      ProcessViewer.this.target = this.task;
      if (ProcessViewer.this.taskDisplayPanel == null)
      {
        String str1 = null;
        Registry localRegistry = Registry.getRegistry("com.teamcenter.rac.common.tcviewer.tcviewer");
        ProcessViewer.this.getViewer().setLoadingDisplay();
        this.task.getSession().setStatus(localRegistry.getString("downloadingViewData"));
        try
        {
          this.task.performAction(100, "");
        }
        catch (Exception localException1)
        {
          MessageBox.post(localException1);
          int[] localObject1 = ((InterfaceExceptionStack)localException1).getErrorCodes();
          int i = localObject1 == null ? 0 : localObject1[0];
          if (i != 33086)
          {
            ProcessViewer.this.getViewer().setNoViewDataFound();
            return;
          }
        }
        String str2 = this.task.getToStringProperty();
        String[] localObject1 = { "process_stage_list", "release_status_list", str2, "object_name", "job_name", "iconKey", "late_flag", "task_type", "wait_for_all_reviewers", "late_flag", "due_date", "object_desc", "viewed_by_me" };
        String[] arrayOfString = { "comments", "task_definition", "root_task", "state", "done", "signoff_quorum", "parent_process" };
        try
        {
          ArrayList localArrayList = new ArrayList(1);
          localArrayList.add(this.task);
          TCComponentType.getPropertiesSet(localArrayList, (String[])localObject1);
          TCComponentType.getTCPropertiesSet(localArrayList, arrayOfString);
        }
        catch (Exception localException2) {}
        try
        {
          str1 = this.task.getTaskType();
          Object localObject4;
          if ((str1.equals("EPMReviewTask")) || (str1.equals("EPMAcknowledgeTask")))
          {
        	  TCComponentTask[] localObject2 = this.task.getSubtasks();
            if ((localObject2 != null) && (localObject2.length > 0))
            {
              Object localObject3 = null;
              localObject4 = null;
              for (Object localObject9 : localObject2)
              {
                localObject3 = localObject9;
                localObject4 = ((TCComponentTask)localObject3).getState();
                if ((localObject4 == TCTaskState.STARTED) || (localObject4 == TCTaskState.PENDING) || (localObject4 == TCTaskState.UNASSIGNED)) {
                  break;
                }
              }
              this.task = ((TCComponentTask)localObject3);
              str1 = this.task.getTaskType();
            }
          }
          String[] localObject2 = localRegistry.getStringArray("TaskPerformViewer.TYPES");
          int j = 0;
          for (Object localObject8 : localObject2) {
            if (str1.equals(localObject8))
            {
              j = 1;
              break;
            }
          }
          Object localObject6;
          if (j != 0)
          {
            localObject4 = localRegistry.getString(str1 + ".VIEWPANEL");
            Object localObject7;
            if (((String)localObject4).equals("com.teamcenter.rac.workflow.commands.adhoc.RouteTaskSignoffsPanel"))
            {
              localObject6 = this.task.getSession();
              localObject7 = null;
              if (localObject6 != null)
              {
            	  TCPreferenceService localObject8 = ((TCSession)localObject6).getPreferenceService();
                localObject7 = ((TCPreferenceService)localObject8).getString(0, "WORKFLOW_new_route_task_panel");
              }
              if ((localObject7 == null) || (((String)localObject7).length() == 0) || (((String)localObject7).trim().toLowerCase().equals("on"))) {
                localObject4 = localRegistry.getString(str1 + ".NEWVIEWPANEL");
              }
            }
            if (localObject4 != null)
            {
              localObject6 = ProcessViewer.this.getViewer();
              localObject7 = AIFUtility.getActiveDesktop();
              ProcessViewer.this.taskDisplayPanel = ((JPanel)Instancer.newInstance((String)localObject4, new Object[] { localObject7, localObject6, this.task }));
            }
          }
          else
          {
            localObject4 = this.task.getTaskForm();
            if (localObject4 != null)
            {
              localObject6 = new FormTaskPanel(this.task);
              ProcessViewer.this.taskDisplayPanel = new JPanel(new BorderLayout());
              ProcessViewer.this.taskDisplayPanel.add("Center", (Component)localObject6);
            }
          }
        }
        catch (Exception localException3)
        {
          ProcessViewer.this.getViewer().setNoViewDataFound();
        }
      }
      if (ProcessViewer.this.taskDisplayPanel != null)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            ProcessViewer.this.removeAll();
            ProcessViewer.this.add("North", ProcessViewer.this.radioPanel);
            ProcessViewer.this.add("Center", ProcessViewer.this.taskDisplayPanel);
            TaskPanel taskPanel = new TaskPanel(task);
            JScrollPane jp = new JScrollPane(
					ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            taskPanel.setBackground(Color.white);
			jp.setViewportView(taskPanel);
			ProcessViewer.this.add(BorderLayout.SOUTH, jp);
            ProcessViewer.this.getViewer().setViewPanel(ProcessViewer.this);
          }
        });
      }
      else
      {
        ProcessViewer.taskView = false;
        ProcessViewer.this.processButton.setSelected(true);
        ProcessViewer.this.setComponent(ProcessViewer.this.target);
      }
    }
  }
  
  class RadioListener
    implements ActionListener
  {
    RadioListener() {}
    
    public void actionPerformed(ActionEvent paramActionEvent)
    {
      if (paramActionEvent.getActionCommand().equals(ProcessViewer.TASK_VIEW)) {
        ProcessViewer.taskView = true;
      } else {
        ProcessViewer.taskView = false;
      }
      ProcessViewer.this.setComponent(ProcessViewer.this.target);
    }
  }
  
  private class SetProcessViewPanelRunnable
    implements Runnable
  {
    private boolean show_radio_panel;
    
    public SetProcessViewPanelRunnable(boolean paramBoolean)
    {
      this.show_radio_panel = paramBoolean;
    }
    
    public void run()
    {
      ProcessViewer.this.removeAll();
      if (this.show_radio_panel) {
        ProcessViewer.this.add("North", ProcessViewer.this.radioPanel);
      }
      ProcessViewer.this.add("Center", ProcessViewer.this.processViewPanel);
      ProcessViewer.this.processViewPanel.open((TCComponent)ProcessViewer.this.target);
      ProcessViewer.this.getViewer().setViewPanel(ProcessViewer.this);
    }
  }
}
